package com.zmj.framework.response;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@ToString
public class Result<T> {
    /**
     * 200成功
     */
    private int code;

    /**
     * 错误说明
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回数据对象 data
     */
    private T result;

    public Result(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    private static <T> Result<T> generate(ResponseCode response, T data) {
        return Result.<T>builder().code(response.getCode()).message(response.getMsg()).data(data).build();
    }

    private static <T> Result<T> generate(int code, String message, T data) {
        return Result.<T>builder().code(code).message(message).data(data).build();
    }

    public static <T> Result<T> success(T data) {
        return generate(ResponseCode.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return generate(ResponseCode.SUCCESS, null);
    }

    public static <T> Result<T> fail() {
        return generate(ResponseCode.FAIL, null);
    }

    public static <T> Result<T> fail(ResponseCode response) {
        return generate(response, null);
    }

    public static <T> Result<T> fail(String msg) {
        return generate(ResponseCode.FAIL.getCode(), msg, null);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return generate(ResponseCode.FAIL.getCode(), msg, data);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return generate(code, msg, null);
    }

    public boolean isOk() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

}
